<?php

/**
 * 会员黑名单
 * File Name：BlacklistModel.php
 * File Encoding：UTF-8
 * File New Time：2014-5-23 9:28:36
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class BlacklistModel extends BaseModel {

    public function tableName() {
        return '{{blacklist}}';
    }

    public function rules() {
        return array(
            array('nick', 'required', 'message' => '请填写旺旺名！'),
            array('nick', 'unique', 'message' => '该旺旺名已经添加过黑名单，无需再次添加！'),
            array('note', 'required', 'message' => '请填写添加黑名单的原因！'),
            array('type,disabled,start_time,end_time,add_time', 'safe'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 获取拉黑类型
     * @return array
     */
    public function getTypeList($str = NULL) {
        $list = array(0 => '永久拉黑', 1 => '自定义拉黑时间');
        if ($str === NULL) {
            return $list;
        } else {
            if (is_string($str)) {//传入字符，表示返回id
                return array_search($str, $list);
            } else {
                return isset($list[$str]) ? $list[$str] : false;
            }
        }
        return array();
    }

    /**
     * 检查旺旺是否在黑名单中
     * @param string $nick 旺旺名
     * @param string $new 是否刷新缓存
     * @return boolean true表示在黑名单，false不在黑名单中
     */
    public function isBlack($nick, $new = false) {
        if (empty($nick)) {
            return false;
        }
        $key = md5('BlacklistModel_isBlack_' . $nick);
        //是否刷新缓存
        if ($new) {
            Yii::app()->cache->delete($key);
        }
        $cache = Yii::app()->cache->get($key);
        if (empty($cache)) {
            $info = $this->find($this->where(array('nick' => $nick)));
            if (empty($info)) {
                return false;
            }
            $cache = $info->attributes;
            //缓存，默认3600 秒
            Yii::app()->cache->set($key, $cache, 3600);
        }
        //判断被类黑类型
        if ((int) $cache['type'] == 1) {//自定义拉黑时间
            //结束时间
            $end_time = strtotime($cache['end_time']);
            //时差
            $endTime = $end_time - time();
            //时差小于0，表示已经到了结束时间
            if ($endTime < 0) {
                Yii::app()->cache->delete($key);
                return false;
            }
            return true;
        } else {
            return true;
        }
    }

    /**
     * 添加黑名单
     * @param type $data
     * @return boolean
     */
    public function addBlack($data = array()) {
        if (empty($data)) {
            $data = $_POST;
        }
        $this->attributes = $data;
        //自定义拉黑时间
        if ($this->type == 1) {
            //起始时间
            if (empty($data['start_time'])) {
                $this->addError('start_time', '请填写拉黑起始时间！');
                return false;
            }
            //结束时间
            if (empty($data['end_time'])) {
                $this->addError('end_time', '请填写拉黑结束时间！');
                return false;
            }
        } else {
            $this->start_time = 0;
            $this->end_time = 0;
        }
        //新增
        $this->setIsNewRecord(true);
        //验证并添加
        if ($this->validate() && $this->save()) {
            return true;
        }
        return false;
    }

    /**
     * 修改信息
     * @param type $data
     * @return boolean
     */
    public function editorBlack($data = array()) {
        if (empty($data)) {
            $data = $_POST;
        }
        $model = $this->findByPk($data['id']);
        if (empty($model)) {
            $this->addError('', '该信息不存在！');
            return false;
        }
        $model->attributes = $data;
        //自定义拉黑时间
        if ($model->type == 1) {
            //起始时间
            if (empty($data['start_time'])) {
                $this->addError('start_time', '请填写拉黑起始时间！');
                return false;
            }
            //结束时间
            if (empty($data['end_time'])) {
                $this->addError('end_time', '请填写拉黑结束时间！');
                return false;
            }
        } else {
            $this->start_time = 0;
            $this->end_time = 0;
        }
        if ($model->validate() && $model->save()) {
            return true;
        }
        $this->addErrors($model->getErrors());
        return false;
    }

    //数据保存前操作
    protected function beforeSave() {
        //时间处理
        if (!is_numeric($this->start_time)) {
            $this->start_time = strtotime($this->start_time);
        }
        if (!is_numeric($this->end_time)) {
            $this->end_time = strtotime($this->end_time);
        }
        //是否新增
        if ($this->isNewRecord) {
            $this->add_time = time();
        } else {
            
        }
        return true;
    }

    //数据保存后回调
    protected function afterSave() {
        //更新缓存
        if ($this->nick) {
            $this->isBlack($this->nick, true);
        }
    }

}
